const Service = require("egg").Service;

class LoginService extends Service {

  //注册
  async register({ accountNumber, passWord, userName, nickName }) {
    const { ctx, app } = this;
    let transaction = null
    try {
      transaction = await ctx.model.transaction();
      const account = await ctx.service.account.findAccount(accountNumber);
      if (account) {
        throw new Error("已经存在")
      } else {
        let account = await app.model.Account.create({
          accountNumber,
          passWord
        }, {
          transaction
        });
        let user = await app.model.User.create({
          userName,
          nickName,
          headPicture: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.alicdn.com%2Fi4%2F2204484777%2FO1CN01l3OVic1l9ufSylfAj_%21%212204484777.jpg&refer=http%3A%2F%2Fimg.alicdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1675046478&t=2f817922272f32dad56eb18044afe38f',
          account_id:account.id
        }, {
          transaction
        });
        await transaction.commit();
        return user
      }
    } catch (error) {
      await transaction.rollback();
      throw new Error(error)
    }
  }

}

module.exports = LoginService;
